package com.ems.servlet;

import com.ems.dao.SalaryDAO;
import com.ems.model.Salary;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/salary")
public class SalaryServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        SalaryDAO salaryDAO = new SalaryDAO();

        if ("list".equals(action)) {
            try {
                List<Salary> salaries = salaryDAO.getAllSalaries();
                request.setAttribute("salaries", salaries);
            } catch (SQLException e) {
                e.printStackTrace();
                request.setAttribute("error", "数据库操作出错，请稍后再试");
            }
        } else if ("edit".equals(action)) {
            String salaryIdStr = request.getParameter("salaryId");
            if (salaryIdStr != null) {
                int salaryId = Integer.parseInt(salaryIdStr);
                try {
                    Salary salary = salaryDAO.getSalaryById(salaryId);
                    request.setAttribute("salary", salary);
                } catch (SQLException e) {
                    e.printStackTrace();
                    request.setAttribute("error", "数据库操作出错，请稍后再试");
                }
            }
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/salary.jsp");
        dispatcher.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        SalaryDAO salaryDAO = new SalaryDAO();

        if ("addSalary".equals(action)) {
            // 现有的添加薪资逻辑
            String empId = request.getParameter("empId");
            double amount = Double.parseDouble(request.getParameter("amount"));
            String payDateStr = request.getParameter("payDate");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date payDate = null;
            try {
                payDate = sdf.parse(payDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
                request.setAttribute("error", "日期格式错误，请使用 yyyy-MM-dd 格式");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/salary.jsp");
                dispatcher.forward(request, response);
                return;
            }

            Salary salary = new Salary();
            salary.setEmpId(empId);
            salary.setAmount(amount);
            salary.setPayDate(payDate);

            try {
                if (salaryDAO.addSalary(salary)) {
                    request.setAttribute("message", "薪资添加成功");
                } else {
                    request.setAttribute("error", "薪资添加失败");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                request.setAttribute("error", "数据库操作出错，请稍后再试");
            }
        } else if ("updateSalary".equals(action)) {
            // 处理修改薪资逻辑
            String salaryIdStr = request.getParameter("salaryId");
            String empId = request.getParameter("empId");
            double amount = Double.parseDouble(request.getParameter("amount"));
            String payDateStr = request.getParameter("payDate");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date payDate = null;

            try {
                payDate = sdf.parse(payDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
                request.setAttribute("error", "日期格式错误，请使用 yyyy-MM-dd 格式");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/salary.jsp");
                dispatcher.forward(request, response);
                return;
            }

            Salary salary = new Salary();
            salary.setSalaryId(Integer.parseInt(salaryIdStr));
            salary.setEmpId(empId);
            salary.setAmount(amount);
            salary.setPayDate(payDate);

            try {
                if (salaryDAO.updateSalary(salary)) {
                    request.setAttribute("message", "薪资修改成功");
                } else {
                    request.setAttribute("error", "薪资修改失败");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                request.setAttribute("error", "数据库操作出错，请稍后再试");
            }
        } else if ("deleteSalary".equals(action)) {
            // 处理删除薪资逻辑
            String salaryIdStr = request.getParameter("salaryId");
            if (salaryIdStr != null && !salaryIdStr.isEmpty()) {
                try {
                    int salaryId = Integer.parseInt(salaryIdStr);
                    if (salaryDAO.deleteSalary(salaryId)) {
                        request.setAttribute("message", "薪资删除成功");
                    } else {
                        request.setAttribute("error", "薪资删除失败，可能是薪资 ID 不存在");
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    request.setAttribute("error", "无效的薪资 ID，请检查输入");
                } catch (SQLException e) {
                    e.printStackTrace();
                    request.setAttribute("error", "数据库操作出错，请稍后再试");
                }
            } else {
                request.setAttribute("error", "未提供有效的薪资 ID");
            }
        }
        try {
            List<Salary> salaries = salaryDAO.getAllSalaries();
            request.setAttribute("salaries", salaries);
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "数据库操作出错，请稍后再试");
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/salary.jsp");
        dispatcher.forward(request, response);
    }
}